<?php
	function getSiteModules($site_id, $set_id=0)
	{
		$i=0;
		if($set_id > 0)
		{ $result = mysql_query('SELECT * FROM '.DB_PERFIX.'modules WHERE site_id='.$site_id.' AND set_id='.$set_id.' ORDER BY mod_id ASC;'); }
		else
		{ $result = mysql_query('SELECT * FROM '.DB_PERFIX.'modules WHERE site_id='.$site_id.' ORDER BY mod_id ASC;'); }
		while($row = mysql_fetch_array($result))
		{
			foreach(array_keys($row) as $key)
			{ $item[$i][$key] = $row[$key]; }
			$i++;
		}
		return $item;
	}
	
	function getModuleAdmin($id)
	{
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."modules WHERE id=".$id.";");
		while($row = mysql_fetch_array($result))
		{
			foreach(array_keys($row) as $key)
			{ $item[$key] = $row[$key]; }
		}
		if ($item['hfb_set'] > 0)
		{
			$result = mysql_query("SELECT * FROM ".DB_PERFIX."hfb_sets WHERE id=".$item['hfb_set'].";");
			while($row = mysql_fetch_array($result))
			{
					$item['com_border'] = $row['border'];
					$item['com_header'] = $row['header'];
					$item['com_footer'] = $row['footer'];
					$item['com_button'] = $row['button'];
					$item['com_hbutton'] = $row['h_button'];
					$item['com_fbutton'] = $row['f_button'];
			}
		}
		return $item;
	}
	function newModule($name_l, $name_r)
	{
		$name_l = stripNonAlphaNum($name_l); 
		$name_r = stripNonAlphaNum($name_r);
		if($name_l != '')
		{
			$insert  = "INSERT INTO ".DB_PERFIX."modules (`id`, `name_l`, `name_r`, `path`, `active`)"; 
			$insert .= "VALUES (NULL, '".$name_l."', '".$name_r."', 'modules/', 0);";
			$result = mysql_query($insert);
			return $result;
		}
	}
	
	function updateModule($id, $set_id, $mod_id, $name_l, $name_r, $hfb_set=0, $path='', $type=0, $max=0, $cols=0, $rows=0, $indexing=0)
	{
		$name_l 	= stripNonAlphaNum($name_l); 
		$name_r 	= stripNonAlphaNum($name_r);
		
		if(!($hfb_set > 0))
		{ $hfb_set = 0; }
		if(!($site_template > 0))
		{ $site_template = 0; }
		
		if($name_l != '')
		{	
			$update  = ' UPDATE '.DB_PERFIX.'modules SET ';
			$update .= ' `set_id` = '.$set_id.', ';
			$update .= ' `mod_id` = '.$mod_id.', ';
			$update .= ' `name_l` = "'.$name_l.'", ';
			$update .= ' `name_r` = "'.$name_r.'", ';
			if($path != '')
			{ $update .= ' `path` = "'.$path.'", '; }
			$update .= ' `type` = '.$type.', ';
			$update .= ' `max` = '.$max.', ';
			$update .= ' `cols` = '.$cols.', ';
			$update .= ' `rows` = '.$rows.', ';
			$update .= ' `indexing` = '.$indexing.', ';
			$update .= ' `hfb_set` = '.$hfb_set.' ';
			$update .= ' WHERE id='.$id.' LIMIT 1; ';
			$result = mysql_query($update);
		}
	}
	
	function deleteModule($id)
	{
		if($id > 0)
		{
			$result = mysql_query('DELETE FROM '.DB_PERFIX.'modules WHERE id='.$id.' LIMIT 1;');
			return $result;
		}
	}
	
	function addModuleToSite($site, $module)
	{
		if(($module > 0) && ($site > 0))
		{
			$mod = getModuleAdmin($module);
			if(is_file(SITE_PATH.$mod['path']))
			{ 
				$path = '';
				$folders = explode('/',$mod['path']);
				if(count($folders) > 0)
				{
					$i=1;
					foreach($folders as $folder)
					{
						if($i < count($folders))
						{ $path .= $folder.'/'; }
						$i++;
					}
					if(!(is_dir(SITE_PATH.'templates/sites/'.$site.'/'.$path)))
					{ mkdir(SITE_PATH.'templates/sites/'.$site.'/'.$path); }
				}
				
				if(!(is_dir(SITE_PATH.'templates/sites/'.$site.'/modules/')))
				{ mkdir(SITE_PATH.'templates/sites/'.$site.'/modules/'); }
				if(!(is_dir(SITE_PATH.'templates/sites/'.$site.'/layout/')))
				{ mkdir(SITE_PATH.'templates/sites/'.$site.'/layout/'); }
				copy(SITE_PATH.$mod['path'], SITE_PATH.'templates/sites/'.$site.'/'.$mod['path']); 
			}
			if($mod['id'] == $module)
			{
				$insert  = 'INSERT INTO '.DB_PERFIX.'modules (';
				$values = 'VALUES (';
				
				$insert .= '`set_id`, ';
				$values .= ''.$mod['set_id'].', ';
				
				$insert .= '`site_id`, ';	
				$values .= ''.$site.', ';
				
				$insert .= '`mod_id`, ';
				$values .= ''.$mod['mod_id'].', ';
				
				$insert .= '`name_l`, ';
				$values .= '"'.$mod['name_l'].'", ';
				
				$insert .= '`name_r`, '; 
				$values .= '"'.$mod['name_r'].'", ';	
				
				$insert .= '`path`, '; 	
				$values .= '"'.$mod['path'].'", ';
				
				$insert .= '`object`, '; 
				$values .= ''.$mod['object'].', ';
				
				$insert .= '`icon`, '; 	
				$values .= ''.$mod['icon'].', ';
				
				$insert .= '`type`, '; 	
				$values .= ''.$mod['type'].', ';
				
				$insert .= '`hfb_set`, '; 	
				$values .= ''.$mod['hfb_set'].', ';
				
				$insert .= '`active`, '; 	
				$values .= ''.$mod['active'].', ';
				
				$insert .= '`max`, '; 	
				$values .= ''.$mod['max'].', ';
				
				$insert .= '`cols`, '; 
				$values .= ''.$mod['cols'].', ';
					
				$insert .= '`rows`, '; 	
				$values .= ''.$mod['rows'].', ';
				
				$insert .= '`indexing`, '; 	
				$values .= ''.$mod['indexing'].', ';
				
				$insert .= '`content_id` ';
				$values .= ''.$mod['content_id'].' ';
				
				$insert .= ')';
				$values .= ');';
				
				$result = mysql_query($insert.$values);
				return $result;
			}
		}
	}
?>